import requests
import json
import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
scheduler = BlockingScheduler()


day = datetime.datetime.now().strftime("%Y-%m-%d")


# 第三方法天气api
def get_weather(api_key, city):
    base_url = "http://api.openweathermap.org/data/2.5/weather"
    params = {
        'q': city,
        'appid': api_key,
        'units': 'metric'
    }
    response = requests.get(base_url, params=params)
    weather_data = response.json()
    return weather_data

# api_key =   # 替换为你的API密钥
# city = 'Changsha'  # 替换为你想查询的城市
weather_data = get_weather(api_key='531b7f1951343b16990631805421a41e', city="Shanghai")
# print(json.dumps(weather_data, indent=4))

# 获取天气
def translate_weather(weather_data):
    weather_dict = {
        "Clear": "晴朗",
        "Clouds": "多云",
        "Rain": "雨",
        "Snow": "雪",
        # 添加更多天气翻译
    }
    weather_description = weather_data['weather'][0]['main']
    weather_description_cn = weather_dict.get(weather_description, "未知")  # 根据返回的value为键,在获取对应的翻译
    return weather_description_cn


# weather_description_cn = translate_weather(weather_data)
# print(f"天气：{translate_weather(weather_data)}")

def work_information(url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=b706d491-ece0-4c12-bda4-518106e7dfd0'):
    # get_weather(api_key='531b7f1951343b16990631805421a41e', city="Shanghai")
    param = {
        "msgtype": "markdown",
        "markdown": {
            "content": f"# <font color='info'>小果提醒 </font>\n\n> **日期**：{day} \n\n>**天气**：<font>{translate_weather(weather_data)}</font>\n\n> 元气满满的一天开始了，小果提醒大家准时打卡的同时也要记得点餐呦"
        }
    }

    r = requests.post(url=url,json=param)  # url中的xxx是你的机器人webhook对应片段，不要轻易泄露出去否则任何人都可以控制你的机器人哦
    print(r.text)


def off_duty_information(url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=b706d491-ece0-4c12-bda4-518106e7dfd0'):
    param = {
        "msgtype": "markdown",
        "markdown": {
            "content": f"<font color='info'>小果感谢大家一天的辛苦付出，下班也要记得打卡哦 </font>"}
    }

    r = requests.post(url=url,json=param)  # url中的xxx是你的机器人webhook对应片段，不要轻易泄露出去否则任何人都可以控制你的机器人哦
    print(r.text)


# 第三方获取2024年中国的公共假期api
def get_holidays(year, country):
    response = requests.get(f'https://date.nager.at/Api/v2/PublicHolidays/{year}/{country}')
    holidays = [datetime.datetime.strptime(holiday['date'], '%Y-%m-%d').date() for holiday in response.json()]
    return holidays

holidays = get_holidays(2024, 'CN')
holiday_list = [datetime.date(2024, 1, 2),
                datetime.date(2024, 2, 10),
                datetime.date(2024, 2, 11),
                datetime.date(2024, 2, 12),
                datetime.date(2024, 2, 13),
                datetime.date(2024, 2, 14),
                datetime.date(2024, 2, 15),
                datetime.date(2024, 2, 16),
                datetime.date(2024, 2, 17),
                datetime.date(2024, 4, 4),
                datetime.date(2024, 4, 5),
                datetime.date(2024, 4, 6),
                datetime.date(2024, 5, 1),
                datetime.date(2024, 5, 5),
                datetime.date(2024, 6, 8),
                datetime.date(2024, 6, 9),
                datetime.date(2024, 6, 10),
                datetime.date(2024, 9, 15),
                datetime.date(2024, 9, 16),
                datetime.date(2024, 9, 17),
                datetime.date(2024, 10, 1),
                datetime.date(2024, 10, 2),
                datetime.date(2024, 10, 3),
                datetime.date(2024, 10, 4),
                datetime.date(2024, 10, 5),
                datetime.date(2024, 10, 6),
                datetime.date(2024, 10, 7)]
all_holidays = holidays + holiday_list   # 共假日加上具体放假天数
print(all_holidays)

def job_01():
    today = datetime.date.today()
    if today not in holidays:
        work_information(url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ed6642d8-42e7-465f-9f31-9936e6673bec')
        print("Today is a holiday, skipping the job.")

def job_02():
    today = datetime.date.today()
    if today not in holidays:
        off_duty_information(url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ed6642d8-42e7-465f-9f31-9936e6673bec')


scheduler.add_job(job_01, 'cron', day_of_week='mon-fri',hour=9, minute=45)

scheduler.add_job(job_02, 'cron', day_of_week='mon-fri',hour=19, minute=0)
scheduler.start()









